<?
require_once("cms/core/action/AbstractListAction.php");
require_once("cms/core/om/BinaryFilePeer.php");

class ListAction extends AbstractListAction {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("ListAction");
    }

    public function activate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Activating galleries");
        $c = new Criteria();
        $c->add(GalleryPeer::GALLERY_ID, $form->getItems(), Criteria::IN);
        $c->add(GalleryPeer::ACTIVE, true);
        try {
            GalleryPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.activate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.activate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function deactivate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deactivating galleries");
        $c = new Criteria();
        $c->add(GalleryPeer::GALLERY_ID, $form->getItems(), Criteria::IN);
        $c->add(GalleryPeer::ACTIVE, false);
        try {
            GalleryPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.deactivate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.deactivate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function delete(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deleting galleries");
        $c = new Criteria();
        $c->add(GalleryPeer::GALLERY_ID, $form->getItems(), Criteria::IN);

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            $galleries = GalleryPeer::doSelect($c, $conn);

            foreach($galleries as $gallery) {
                $galleryImages = $gallery->getGalleryImages(null, $conn);
                foreach($galleryImages as $galleryImage) {

                    $thumb = $galleryImage->getImageRelatedByThumbnailId();
                    $image = $galleryImage->getImageRelatedByImageId();

                    GalleryImagePeer::doDelete($galleryImage, $conn);
                    BinaryFilePeer::doDelete($image->getBinaryFile(), $conn);
                    BinaryFilePeer::doDelete($thumb->getBinaryFile(), $conn);

                }
            }

            GalleryPeer::doDelete($c, $conn);

            $conn->commit();

            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.delete", true));

        } catch(PropelException $e) {
            $conn->rollback();
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.delete", true));
            $this->log->error($e->getMessage());
        }

    }

}
?>